package com.hhkj.admin.bank.service.extend.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hhkj.admin.bank.common.cont.RuleConst;
import com.hhkj.admin.bank.domain.BankRule;
import com.hhkj.admin.bank.model.dopo.BankRuleDo;
import com.hhkj.admin.bank.model.dopo.BankRulePo;
import com.hhkj.admin.bank.service.base.BankRuleService;
import com.hhkj.admin.bank.service.extend.BankRuleExtendService;
import com.hhkj.admin.course.domain.Course;
import com.hhkj.admin.course.service.extend.CourseExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * BankRuleServiceExtendImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/6/8 14:44
 */
@Service
public class BankRuleExtendServiceImpl
        extends BaseExtendServiceImpl<BankRule, BankRuleDo, BankRulePo, BankRuleService>
        implements BankRuleExtendService
{
    @Autowired
    private CourseExtendService courseExtendService;

    @Override
    public void removeByCourseParentIdAndCourseBankTypeId(Long courseParentId, Long courseBankTypeId)
    {
        LambdaQueryWrapper<BankRule> ruleQuery = new LambdaQueryWrapper<>();
        ruleQuery.eq(BankRule::getCourseParentId, courseParentId)
                .eq(BankRule::getCourseBankTypeId, courseBankTypeId);
        this.getService().remove(ruleQuery);
    }

    @Override
    public BankRulePo getPoByCourseIdAndCourseBankTypeIdNotNull(Long courseId, Long courseBankTypeId)
    {
        Course coursePo = courseExtendService.getByIdNotNull(courseId);
        BankRulePo rulePo
                = this.getPoByCourseParentIdAndCourseBankTypeId(coursePo.getParentId(), courseBankTypeId);
        this.checkNull(rulePo);
        if (rulePo.hasCourseChildrenRule()) {
            rulePo = this.getPoByCourseIdAndCourseBankTypeId(courseId, courseBankTypeId);
            this.checkNull(rulePo);
        }
        return rulePo;
    }

    @Override
    public BankRulePo getPoByCourseIdAndCourseBankTypeId(Long courseId, Long courseBankTypeId)
    {
        BankRuleDo conditionDo = new BankRuleDo();
        conditionDo.setTypeCode(RuleConst.Type.SINGLE.getCode());
        conditionDo.setCourseId(courseId);
        conditionDo.setCourseBankTypeId(courseBankTypeId);
        return this.getService().getPoByDo(conditionDo);
    }

    @Override
    public BankRulePo getPoByCourseParentIdAndCourseBankTypeId(Long courseParentId, Long courseBankTypeId)
    {
        BankRuleDo conditionDo = new BankRuleDo();
        conditionDo.setCourseParentId(courseParentId);
        conditionDo.setCourseBankTypeId(courseBankTypeId);
        return this.getService().getPoByDo(conditionDo);
    }

    @Override
    public boolean removeByParentId(Long parentId)
    {
        return this.getService().remove(
                new LambdaQueryWrapper<BankRule>()
                        .eq(BankRule::getParentId, parentId));
    }

    @Override
    public List<BankRulePo> listPoByParentId(Long parentId)
    {
        BankRuleDo bankRuleDo = new BankRuleDo();
        bankRuleDo.setTypeCode(RuleConst.Type.SINGLE.getCode());
        bankRuleDo.setParentId(parentId);
        return this.getService().listPoByDo(bankRuleDo);
    }

    /**
     * 检验是为空
     *
     * @param rulePo 抽题规则信息
     */
    private void checkNull(BankRule rulePo)
    {
        if (ObjectUtil.isNull(rulePo)) {
            throw new GlobalException("抽题规则信息不存在");
        }
    }
}
